package com.maezia.tdr.dao.repository;

import com.alibaba.fastjson.JSONObject;
import com.maezia.tdr.common.enums.RedisMessageEnum;
import com.maezia.tdr.domain.dto.ReportInfoDto;
import com.maezia.tdr.domain.po.Dict;
import com.maezia.tdr.domain.vo.ReportVo;
import com.maezia.tdr.service.DictService;
import com.maezia.tdr.common.CommonConstant;
import com.maezia.tdr.common.redis.BaseRedisRepository;
import com.maezia.tdr.dao.mapper.TestDriveReportMapper;
import com.maezia.tdr.domain.po.TestDriveReport;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Repository
public class ReportRepository extends BaseRedisRepository<ReportVo> {

    private static String REPORT_INFO_DATA_EXPIRED = "REPORT_INFO_DATA_EXPIRED";

    @Autowired
    private DictService dictService;

    @Autowired
    private TestDriveReportMapper testDriveReportMapper;

    public ReportRepository() {
        super(ReportVo.class);
    }

    public void saveReportVo(ReportVo report){
        String redisKey = REPORT_INFO_DATA_EXPIRED + CommonConstant.COLON + report.getId();
        if(!redisTemplateTemporary.hasKey(redisKey)){
            Dict dict = dictService.getByKey(REPORT_INFO_DATA_EXPIRED);
            redisValueOpsTemporary.set(redisKey, report, Long.valueOf(dict.getValue()), TimeUnit.HOURS);
        }
    }

    public void refreshReportCache(TestDriveReport report){
        deleteByMd5(report.getMd5Value());
        saveReportVo(buildReportVo(report));
    }

    public ReportVo getReportVo(String md5) {
        String redisKey = REPORT_INFO_DATA_EXPIRED + CommonConstant.COLON + md5;
        ReportVo reportVo = redisValueOpsTemporary.get(redisKey);
        if(StringUtils.isEmpty(reportVo)){
            TestDriveReport reportInfo = testDriveReportMapper.getByMd5(md5);
            if(!ObjectUtils.isEmpty(reportInfo)){
                reportInfo.setMd5Value(md5);
                reportVo = buildReportVo(reportInfo);
                saveReportVo(reportVo);
            }
        }
        return reportVo;
    }

    public void sendMessage(ReportInfoDto reportInfo) {
        stringRedisTemplate.convertAndSend(RedisMessageEnum.REPORT_POCESSING_SERVICE.getName(), JSONObject.toJSONString(reportInfo));
    }

    public void deleteByMd5(String md5){
        String redisKey = REPORT_INFO_DATA_EXPIRED + CommonConstant.COLON + md5;
        redisTemplateTemporary.delete(redisKey);
    }

    private ReportVo buildReportVo(TestDriveReport report) {
        ReportVo reportVo = new ReportVo();
        reportVo.setId(report.getMd5Value());

        ReportVo.Driving driving = new ReportVo.Driving();
        BeanUtils.copyProperties(report, driving);
        driving.setRouteHtmlURL(report.getRouteHtmlUrl());
        driving.setRoutePictureURL(report.getRoutePictureUrl());
        reportVo.setDriving(driving);

//        ReportVo.Connectivity connectivity = new ReportVo.Connectivity();
//        connectivity.setCarControlSDSTimes(report.getCarControlSdsTimes());
//        connectivity.setNavigationSDSTimes(report.getNavigationSdsTimes());
//        connectivity.setNotBigLanguageTimes(report.getNotBigLanguageTimes());
//        connectivity.setBigLanguageTimes(report.getBigLanguageTimes());
//        connectivity.setMusicWorkingDuration(report.getMusicWorkingDuration());
//        connectivity.setMusicWorkingPercent(report.getMusicWorkingPercent());
//        connectivity.setIsAvatarStore(report.getIsAvatarStore());
//
//        ReportVo.ConnectivityApp app = new ReportVo.ConnectivityApp();
//        app.setUseTimes(report.getUseAppTimes());
//        app.setUseKTVTimes(report.getUseKtvTimes());
//        app.setUseAppletTimes(report.getUseAppletTimes());
//        app.setUseMediaCenterTimes(report.getUseMediaCenterTimes());
//        app.setUseAvatarStoreTimes(report.getUseAvatarStoreTimes());
//        connectivity.setApp(app);
//        reportVo.setConnectivity(connectivity);

        ReportVo.ParkingInfo parkingInfo = new ReportVo.ParkingInfo();
        parkingInfo.setRememberParkingDuration(report.getRememberParkingDuration());
        parkingInfo.setRememberParkingTimes(report.getRememberParkingTimes());
        parkingInfo.setRememberParkingDistance(report.getRememberParkingDistance());
        parkingInfo.setParkingIPATimes(report.getParkingIpaTimes());
        parkingInfo.setParkingIPADuration(report.getParkingIpaDuration());
        parkingInfo.setParkingIPADistance(report.getParkingIpaDistance());
//        parkingInfo.setParkingRPATimes(report.getParkingRpaTimes());
//        parkingInfo.setParkingRPADuration(report.getParkingRpaDuration());
//        parkingInfo.setParkingRPADistance(report.getParkingRpaDistance());
        reportVo.setParking(parkingInfo);

        reportVo.setCharging(ObjectUtils.isEmpty(report.getCharging()) ? false : report.getCharging().intValue() == 1 ? true : false);
        return reportVo;
    }
}
